clear
set more off
**Aaron Flaaen
**August 11, 2017
**Last Updated:

********************************************************************************
/* SUMMARY OF THIS FILE
FILES THAT MUST BE RUN FIRST: 


	
OUTPUT: analysisdata_manuf.dta						      */
********************************************************************************

global OTHER $dir
global DATA  $dir
global BFPNDATA  $dir

cd $DATA

**--------------------------------------------------------------------------
**Step 1: Create Industry for Firm (From LBD)
**--------------------------------------------------------------------------

**Need to Attach Firm Industry to our LBD-DCA dataset. I propose we use same method as in old project

forvalues yr=2009(1)2012 {
 
         !gunzip $BFPNDATA/lbd`yr'c.dta.gz
 
         use $BFPNDATA/lbd`yr'c.dta, clear
	 
	  **For single-units, replace firmid==cfn (for lfttd)
         replace firmid=cfn if mu==0
	 
         keep firmid emp bestnaics
         drop if firmid==""
 
 
         **Naics Variables
         gen naics2 = substr(bestnaics,1,2)
         gen naics3 = substr(bestnaics,1,3)
         gen naics4 = substr(bestnaics,1,4)
         gen naics5 = substr(bestnaics,1,5)
         gen naics6 = substr(bestnaics,1,6)
         gen naics7 = substr(bestnaics,1,7)
         gen naics8 = substr(bestnaics,1,8)
 
 
         **Determine Firm Industry
         bys firmid naics2: egen sumemp = sum(emp)
         bys firmid: egen maxemp = max(sumemp)
         gen naics2max = naics2 if maxemp==sumemp
         drop sumemp maxemp
         forvalues i = 3(1)8 {
                 local j = `i'-1
                 bys firmid naics`i': egen sumemp = sum(emp) if naics`j'max~=""
                 bys firmid: egen maxemp = max(sumemp)
                 gen naics`i'max = naics`i' if maxemp==sumemp
                 drop sumemp maxemp
         }
 
         drop naics2max naics3max naics4max naics5max naics6max naics7max
         rename naics8max naics_code
 
         collapse (firstnm) naics_code, by(firmid)
         sort firmid
         save firmnaics`yr'.dta, replace
	 !gzip $BFPNDATA/lbd`yr'c.dta
 
 }
 
**--------------------------------------------------------------------------
**Step 2: Bring in DCA_LBD_ Data with Country Identifiers
**--------------------------------------------------------------------------
 
**We will be using the lbd_dca_`i'_clean.dta datasets for LBD-MULT-Country-Parent Info
** See lbd_dca_prep.do for details (modified from  our dca_lbd_createsample.do for offshoring)


**For years 2009 through 2011 we can use the files as they are, just attaching firm-industry
 forvalues i = 2009(1)2011 {
cap !gunzip lbd_dca_`i'_firm.dta.gz
!gunzip lbd_dca_`i'_clean.dta.gz

use lbd_dca_`i'_clean.dta, clear

gen mu = 0
replace mu = 1 if substr(firmid,7,.)=="0000"

collapse (sum) emp pay (firstnm) cfn, by(firmid mu flag_us_mult flag_for_mult ifscode)

**For single-units, replace firmid==cfn (for lfttd)
replace firmid=cfn if mu==0

drop if firmid==""

**Some single-unit cfns that had a missing cfn
collapse (sum) emp pay (max) mu flag_us_mult flag_for_mult ifscode, by(firmid)

merge 1:1 firmid using firmnaics`i'.dta
drop if _m==2
drop _m

save lbd_dca_`i'_firm.dta, replace
!gzip lbd_dca_`i'_firm.dta
}


**For year 2012, best we can do is attach mult flags, etc, from 2011 (we want to keep these constant anyway)
!gunzip $BFPNDATA/lbd2012c.dta.gz
cap !gunzip lbd_dca_2012_firm.dta.gz
use $BFPNDATA/lbd2012c.dta, clear

keep firmid cfn emp pay mu
collapse (sum) emp pay (firstnm) cfn, by(firmid mu)

**For single-units, replace firmid==cfn (for lfttd)
replace firmid=cfn if mu==0

drop if firmid==""

**Some single-unit cfns that had a missing cfn
collapse (sum) emp pay (max) mu, by(firmid)

gen alpha = substr(firmid,1,6)
gen year = 2012

**We will be using multinational identifiers from 2011
merge 1:1 firmid using lbd_dca_2011_firm.dta
drop if _m==2
drop _m

replace flag_us_mult = 0 if flag_us_mult==.
replace flag_for_mult = 0 if flag_for_mult==.
replace ifscode = 111 if ifscode==.
drop naics_code


**We'd like to keep the firmnaics constant into 2012 if possible
merge 1:1 firmid using firmnaics2011.dta
drop if _m==2

**For those that didn't match (new in 2012), now use 2012
preserve
keep if _m==1
drop _m
merge 1:1 firmid using firmnaics2012.dta
drop if _m==2
drop _m
save temp_2012_ind.dta
restore

drop if _m==1
append temp_2012_ind.dta
drop _m


save lbd_dca_2012_firm.dta, replace
!gzip lbd_dca_2012_firm.dta

!gzip $BFPNDATA/lbd2012c.dta



**--------------------------------------------------------------------------
**Step 3: Bring in CMF-Products File and create Int/Final Distinctions
**--------------------------------------------------------------------------


**take survu_id from 2007 and attach industry
foreach num of numlist 2007 {
	!gunzip lbd`num'c.dta.gz
	use lbd`num'c.dta, clear
	rename cfn survu_id
	keep survu_id lbdnum bestnaics
	bys survu_id: gen dup = _N
	drop if dup>1
	drop dup
	sort survu_id
	save lbd`num'temp.dta, replace

	use cmf_prod_naics_`num'.dta, clear
	sort survu_id
	merge survu_id using lbd`num'temp.dta
	tab _m
	keep if _m==3
	drop _m
	rename bestnaics naics_code
	save cmf_2007_prod_finalgoods.dta, replace
	!gzip lbd`num'c.dta
	erase lbd`num'temp.dta
}




**Step 3.2 Generate 2007 Prod Thresholds for 8-Digit Naics, and 7-Digit, 6 Digit
use cmf_2007_prod_finalgoods.dta, clear
**Base is 8-Digit Industry
collapse (sum) prod, by(naics_code baseroot)
sort naics_code
bys naics_code: egen prod_tot = sum(prod)
gen ratio = prod/prod_tot
*Label as Final Good if one product occupies more than 10% of Total
gen final_pot = 0
replace final_pot = 1 if ratio>0.10
bys naics_code: egen num_final_pot = sum(final_pot)
sort naics_code
save cmf_2007_prod_finalgoods_temp.dta, replace


**Collapse by 6-Digit, 5-Digit, 4-Digit Industry, in the event we can't find a 
** match fat the  8-digit level
forvalues j = 6(-1)4 {
	use cmf_2007_prod_finalgoods.dta, clear
	gen naics_code`j' = substr(naics_code,1,`j')
	collapse (sum) prod, by(naics_code`j' baseroot)
	bys naics_code`j': egen prod_tot = sum(prod)
	gen ratio = prod/prod_tot

	**Label as Final Good if one product occupies more than 10% of Total
	gen final_pot = 0
	replace final_pot = 1 if ratio>0.10
	bys naics_code`j': egen num_final_pot = sum(final_pot)
	sort naics_code`j'
	save cmf_2007_prod_finalgoods_temp`j'.dta, replace
}


**Step 3.3 Year 2009-2012
forvalues yr = 2009(1)2012 {

	cap !gunzip dca_lbd_`yr'_intprod.dta.gz
	use lbd_dca_`yr'_firm.dta, clear
	keep naics_code
	duplicates drop
	
	drop if substr(naics_code,1,1)~="3"
	sort naics_code
	merge naics_code using cmf_2007_prod_finalgoods_temp.dta
	tab _m
	
	**If can't find an industry at 8-digit level in CMF, find one at 
	** Less disaggregated level.
	preserve
	keep if _m==3
	drop _m
	save temp_8digit.dta, replace
	restore
	keep if _m==1
	drop _m
	forvalues j = 6(-1)4 {
		keep naics_code
		gen naics_code`j' = substr(naics_code,1,`j')
		collapse (first) naics_code, by(naics_code`j')	
		sort naics_code`j'
		merge naics_code`j' using cmf_2007_prod_finalgoods_temp`j'.dta
		tab _m
		preserve
		keep if _m==3
		drop _m
		save temp_`j'digit.dta, replace
		restore
		keep if _m==1
		drop _m
		drop naics_code`j'
	}
	append using temp_4digit.dta
	append using temp_5digit.dta
	append using temp_6digit.dta
	append using temp_8digit.dta
	
	*keep naics_code prod prod_tot ratio final_pot num_final_pot alpha
	drop naics_code6 naics_code5 naics_code4
	sort naics_code
	save ind_prod_temp.dta, replace

	use lbd_dca_`yr'_firm.dta, clear
	keep firmid naics_code
	drop if substr(naics_code,1,1)~="3"
	sort naics_code
	
	**Cartesian Product!!
	joinby naics_code using ind_prod_temp.dta
		
	sort firmid baseroot
	save dca_lbd_`yr'_intprod.dta, replace
	!gzip dca_lbd_`yr'_intprod.dta
	
}
erase temp_4digit.dta 
erase temp_5digit.dta
erase temp_6digit.dta
erase temp_8digit.dta
erase ind_prod_temp.dta
erase cmf_2007_prod_finalgoods_temp6.dta
erase cmf_2007_prod_finalgoods_temp5.dta
erase cmf_2007_prod_finalgoods_temp4.dta


**--------------------------------------------------------------------------
**Step 4: Bring in CMF-Products File and create Int/Final Distinctions
**--------------------------------------------------------------------------
*/
**Step 4.1 Prep LFTTD Data
forvalues i = 2009(1)2012 {
	foreach ddd in exp imp {
		!gunzip `ddd'_mon_`i'.dta.gz
		use `ddd'_mon_`i'.dta, clear
		rename naics baseroot
	
		replace nonrelvalue = 0 if nonrelvalue==.
		replace relvalue = 0 if relvalue==.
		
		gen jpn = 0
		replace jpn = 1 if country=="xxxx"
		gen na = 0
		replace na = 1 if country=="xxxx" | country=="xxxx"
		gen njpn = 0
		replace njpn = 1 if country~="xxxx"
	
		gen quarter = 1
		replace quarter = 2 if month==4 | month==5 | month==6
		replace quarter = 3 if month==7 | month==8 | month==9
		replace quarter = 4 if month==10 | month==11 | month==12
		
		preserve
		keep if jpn == 1
		drop jpn
				
		collapse (sum) relvalue nonrelvalue, by(firmid baseroot quarter)
		rename relvalue j`ddd'_rel
		rename nonrelvalue j`ddd'_nrel
		save j`ddd'_temp.dta, replace
		restore
	
		preserve
		drop if jpn == 1
		collapse (sum) relvalue nonrelvalue, by(firmid baseroot quarter)
		rename relvalue nj`ddd'_rel
		rename nonrelvalue nj`ddd'_nrel
		save nj`ddd'_temp.dta, replace
		restore
	
		keep if na == 1
		collapse (sum) relvalue nonrelvalue, by(firmid baseroot quarter)
		rename relvalue na`ddd'_rel
		rename nonrelvalue na`ddd'_nrel
		save na`ddd'_temp.dta, replace
	
		merge 1:1 firmid baseroot quarter using nj`ddd'_temp.dta
		drop _m
		merge 1:1 firmid baseroot quarter using j`ddd'_temp.dta
		drop _m
	
		save `ddd'_quarterly_`i'_temp.dta, replace
		keep firmid baseroot
		duplicates drop
		save `ddd'_`i'_temp.dta, replace
		!gzip `ddd'_mon_`i'.dta
	}
	
}
	
	


**Step 4.2: Merge Together and Collapse by Type
forvalues i = 2009(1)2012 {
	foreach ddd in exp imp {
		!gunzip dca_lbd_`i'_intprod.dta.gz
		use dca_lbd_`i'_intprod.dta, clear
		merge m:1 firmid baseroot using `ddd'_`i'_temp
		tab _m
		drop if _m==1
		drop _m
		replace final_pot = 0 if final_pot==.
		replace ratio = 0 if ratio==.
		
		merge 1:m firmid baseroot using `ddd'_quarterly_`i'_temp.dta
		

		collapse (sum) na`ddd'_nrel na`ddd'_rel j`ddd'_nrel j`ddd'_rel nj`ddd'_nrel nj`ddd'_rel  , by(firmid final_pot quarter)
		reshape wide na`ddd'_rel na`ddd'_nrel j`ddd'_rel j`ddd'_nrel nj`ddd'_rel nj`ddd'_nrel, i(firmid quarter) j(final_pot)
		foreach vname in na`ddd'_rel na`ddd'_nrel j`ddd'_rel j`ddd'_nrel nj`ddd'_rel nj`ddd'_nrel {
			rename `vname'0 `vname'_int
			rename `vname'1 `vname'_fin
		}
		
		save quart_`ddd'_`i'_temp2.dta, replace
		!gzip dca_lbd_`i'_intprod.dta.gz
	}
}



**Step 4.3 Clean up Unnecessary Files
foreach ddd in exp imp {
	forvalues i = 2009(1)2012 {
		erase `ddd'_quarterly_`i'_temp.dta
		erase `ddd'_`i'_temp.dta
		!gzip `ddd'_quart_`i'.dta
	}
	erase j`ddd'_temp.dta
	erase nj`ddd'_temp.dta
	erase na`ddd'_temp.dta	
}



**--------------------------------------------------------------------------
**Step 5: Saturate, Create Other variables
**--------------------------------------------------------------------------

************************************************************************************
**AF: 08/17: We'll need to create this file. 
************************************************************************************


**4.1 Saturate the quarterly Panel
forvalues j = 2009(1)2012 {
	use quart_exp_`j'_temp2.dta, clear
	keep firmid
	duplicates drop
	save replicate_`j'.dta, replace
	gen quarter = 1
	compress
	forvalues i = 2(1)4 {
		append using replicate_`j'.dta
		replace quarter= `i' if quarter==.
	}
	sort firmid quarter
	merge firmid quarter using quart_exp_`j'_temp2.dta
	drop _m
	save quart_exp_`j'_temp3.dta, replace
	erase quart_exp_`j'_temp2.dta
	erase replicate_`j'.dta

	use quart_imp_`j'_temp2.dta, clear
	keep firmid
	duplicates drop
	save replicate_`j'.dta, replace
	gen quarter = 1
	compress
	forvalues i = 2(1)4 {
		append using replicate_`j'.dta
		replace quarter= `i' if quarter==.
	}
	sort firmid quarter
	merge firmid quarter using quart_imp_`j'_temp2.dta
	drop _m
	save quart_imp_`j'_temp3.dta, replace
	erase quart_imp_`j'_temp2.dta
	erase replicate_`j'.dta
}

**4.2 Generate extra variables (collapsing int/fin rel/nrel distinctions)
forvalues j = 2009(1)2012 {
	use quart_exp_`j'_temp3.dta, clear 
	
	foreach var of varlist jexp_rel_int jexp_rel_fin jexp_nrel_int jexp_nrel_fin {
		replace `var' = 0 if `var'==.
	}
	foreach var of varlist njexp_rel_int njexp_rel_fin njexp_nrel_int njexp_nrel_fin {
		replace `var' = 0 if `var'==.
	}
	foreach var of varlist naexp_rel_int naexp_rel_fin naexp_nrel_int naexp_nrel_fin {
		replace `var' = 0 if `var'==.
	}
	
	gen naexp = naexp_rel_int + naexp_nrel_int + naexp_rel_fin + naexp_nrel_fin
	gen naexp_rel = naexp_rel_int + naexp_rel_fin
	gen naexp_nrel = naexp_nrel_int + naexp_nrel_fin
	gen naexp_int = naexp_rel_int + naexp_nrel_int
	gen naexp_fin = naexp_rel_fin + naexp_nrel_fin
	
	gen jexp = jexp_rel_int + jexp_nrel_int + jexp_rel_fin + jexp_nrel_fin
	gen jexp_rel = jexp_rel_int + jexp_rel_fin
	gen jexp_nrel = jexp_nrel_int + jexp_nrel_fin
	gen jexp_int = jexp_rel_int + jexp_nrel_int
	gen jexp_fin = jexp_rel_fin + jexp_nrel_fin
	
	gen njexp = njexp_rel_int + njexp_nrel_int + njexp_rel_fin + njexp_nrel_fin
	gen njexp_rel = njexp_rel_int + njexp_rel_fin
	gen njexp_nrel = njexp_nrel_int + njexp_nrel_fin
	gen njexp_int = njexp_rel_int + njexp_nrel_int
	gen njexp_fin = njexp_rel_fin + njexp_nrel_fin
	
	
	
	
	save quart_exp_`j'_temp4.dta, replace 
	erase quart_exp_`j'_temp3.dta
}

forvalues j = 2009(1)2012 {
	use quart_imp_`j'_temp3.dta, clear 
	
	
	foreach var of varlist jimp_rel_int jimp_nrel_int jimp_rel_fin jimp_nrel_fin {
		replace `var' = 0 if `var'==.
	}
	foreach var of varlist njimp_rel_int njimp_nrel_int njimp_rel_fin njimp_nrel_fin {
		replace `var' = 0 if `var'==.
	}
	foreach var of varlist naimp_rel_fin naimp_rel_int naimp_nrel_fin naimp_nrel_int {
		replace `var' = 0 if `var'==.
	}
		
	gen njimp = njimp_rel_int + njimp_nrel_int + njimp_rel_fin + njimp_nrel_fin
	gen njimp_rel = njimp_rel_int + njimp_rel_fin
	gen njimp_nrel = njimp_nrel_int + njimp_nrel_fin
	gen njimp_int = njimp_rel_int + njimp_nrel_int
	gen njimp_fin = njimp_rel_fin + njimp_nrel_fin
	gen jimp = jimp_rel_int + jimp_nrel_int + jimp_rel_fin + jimp_nrel_fin
	gen jimp_rel = jimp_rel_int + jimp_rel_fin
	gen jimp_nrel = jimp_nrel_int + jimp_nrel_fin
	gen jimp_int = jimp_rel_int + jimp_nrel_int
	gen jimp_fin = jimp_rel_fin + jimp_nrel_fin
	gen naimp_int = naimp_rel_int + naimp_nrel_int
	gen naimp_fin = naimp_rel_fin + naimp_nrel_fin
	
	*drop jimp_rel_fin jimp_nrel_fin
	*drop njimp_rel_int njimp_nrel_int njimp_rel_fin njimp_nrel_fin
	*drop naimp_rel_fin naimp_rel_int naimp_nrel_fin naimp_nrel_int
	
	
	save quart_imp_`j'_temp4.dta, replace 
	erase quart_imp_`j'_temp3.dta
}


forvalues j = 2009(1)2012 {
	use quart_exp_`j'_temp4.dta, clear
	gen year = `j'
	sort firmid quarter
	save exp_firm`j'quarterly_clean.dta, replace
	erase quart_exp_`j'_temp4.dta
	
	use quart_imp_`j'_temp4.dta, clear
	gen year = `j'
	sort firmid quarter
	save imp_firm`j'quarterly_clean.dta, replace
	erase quart_imp_`j'_temp4.dta
}


**--------------------------------------------------------------------------
**Step 5: Create Dataset of quarterly Exports, Imports and Firm-Ownership
**--------------------------------------------------------------------------


**5.1: Bring in Quarterly Employment and Payroll
forvalues i = 2009(1)2012 {
	!gunzip ssl_lfttd_`i'.dta.gz
	use ssl_lfttd_`i'.dta, clear

	collapse (firstnm) emp_q1 emp_q2 emp_q3 emp_q4 pay_q1 pay_q2 pay_q3 pay_q4, by(firmid)

	keep emp_q* pay_q* firmid 
	reshape long emp_ pay_, i(firmid) j(qvar) string
	keep firmid qvar pay_ emp_
	rename emp_ emp
	rename pay_ pay
	gen quarter = 1
	replace quarter = 2 if qvar=="q2"
	replace quarter = 3 if qvar=="q3"
	replace quarter = 4 if qvar=="q4"
	sort firmid quarter
	save ssl_emppay`i'.dta, replace
	!gzip ssl_lfttd_`i'.dta
}

**-----------------------------------------------------
**5.2 Prep DCA_LBD Data
forvalues j = 2009(1)2012 {
	cap !gunzip lbd_dca_`j'_firm.dta.gz
	use lbd_dca_`j'_firm.dta, clear

	*Reduce Variables
	*drop flag_for_mult
	gen japan = 0
	replace japan = 1 if ifscode==158
	compress
	drop emp pay
	**Reduce Sample
	**Or: Keep only Manufacturing
	gen naics2 = substr(naics_code,1,2)
	keep if naics2=="xx" | naics2=="xx" | naics2=="xx"
	drop naics2

	save dca_lbd_`j'_temp.dta, replace
	**Create Quarterly Copies
	gen quarter = 1
	compress
	forvalues i = 2(1)4 {
		append using dca_lbd_`j'_temp.dta
		replace quarter= `i' if quarter==.
	}
	sort firmid quarter

**5.22 Merge in Employment/Payroll info
	merge firmid quarter using ssl_emppay`j'.dta
	drop if _m==2
	drop _m
	sort firmid quarter
	save dca_lbd_`j'_quarter.dta, replace

	**5.2 Merge Together Exports and Imports Files
	use imp_firm`j'quarterly_clean.dta, clear
	merge firmid quarter using exp_firm`j'quarterly_clean.dta
	drop _m

	**5.3 Now Merge Together With Quarterly Version of DCA_LBD
	compress
	sort firmid quarter
	merge firmid quarter using dca_lbd_`j'_quarter.dta
	tab _m	
	**_m==1 ==> Non-Manufacturing Firms that Trade
	**_m==2 ==> Manufacturing Firms that Don't Trade
	**_m==3 ==> Manufacturing Firms that Trade
	drop if _m==1
	drop _m
	capture destring year, replace
	replace year = `j'
	compress
	save DCA_LBD_LFTTD_`j'_quarter.dta, replace
	
	!gzip lbd_dca_`j'_firm.dta
}
**-----------------------------------------------------

erase ssl_emppay2009.dta
erase ssl_emppay2010.dta
erase ssl_emppay2011.dta
erase ssl_emppay2012.dta



use DCA_LBD_LFTTD_2009_quarter.dta
**5.4 Stack 2010 and 2011 (and 2009?)
append using DCA_LBD_LFTTD_2010_quarter.dta
append using DCA_LBD_LFTTD_2011_quarter.dta

**Only keep if the firm is present in 2009, 2010, and 2011
bys firmid: gen copies = _N
tab copies
drop if copies<xx
drop copies
compress

**Now add 2012?
append using DCA_LBD_LFTTD_2012_quarter.dta



**Fixes to Firm-Level Indicators
**For These: Enforce all indicators to be japanese, foreign mult

	qui replace japan = 1 if firmid=="`fname'"
	qui replace flag_us_mult=0 if firmid=="`fname'"
	qui replace flag_for_mult=1 if  firmid=="`fname'"
}

	qui replace japan = 1 if firmid=="`fname'"
	qui replace flag_us_mult=0 if firmid=="`fname'"
	qui replace flag_for_mult=1 if firmid=="`fname'"
}


	qui replace japan = 1 if firmid=="`fname'"
	qui replace flag_us_mult=0 if firmid=="`fname'"
	qui replace flag_for_mult=1 if firmid=="`fname'"
}

**For These: Enforce all indicators to be non-japanese

	qui replace japan=0 if firmid=="`fname'"
}

replace flag_for_mult=1 if firmid == ""
replace flag_for_mult=0 if firmid==""
replace flag_for_mult=0 if firmid==""
replace flag_us_mult=1 if firmid==""
replace flag_for_mult=0 if firmid==""
replace flag_us_mult=1 if firmid==""
replace flag_for_mult=0 if firmid==""
replace flag_for_mult=0 if firmid==""
replace flag_us_mult=1 if firmid==""
replace flag_for_mult=0 if firmid==""

**For the rest with disagreements: replace as non-jpn
bys firmid: egen minjpn = min(japan)
replace japan = 0 if minjpn~=japan
drop minjpn


**5.8 Modify Variables and Save
foreach var of varlist naimp_int jimp njimp jimp_int njimp_int jimp_fin njimp_fin jimp_rel njimp_rel jimp_nrel njimp_nrel {
	replace `var' = 0 if `var'==.
	
}

foreach var of varlist naexp_nrel_int naexp_nrel_fin jimp_rel_int jimp_nrel_int  {
	replace `var' = 0 if `var'==.
}

foreach var of varlist naexp naexp_rel naexp_rel_int naexp_rel_fin naexp_int naexp_nrel naexp_fin {
	replace `var' = 0 if `var'==.
}

foreach var of varlist njexp njexp_rel njexp_int njexp_nrel njexp_fin {
	replace `var' = 0 if `var'==.
}


cap gunzip analysisdata_manuf_Q.dta.gz
save analysisdata_manuf_Q.dta, replace
!gzip analysisdata_manuf_Q.dta


**--------------------------------------------------------------------------
**Step 5: Bring in Uniworld Data, do some checks
**--------------------------------------------------------------------------

sort firmid
merge firmid using DFFOUS_2008_2011_ids.dta
tab _m
drop if _m==2
drop _m


**Uniworld Sample Included
replace japanU2008 = 0 if japan==0 & flag_for_mult==1
replace japanU2011 = 0 if japan==0 & flag_for_mult==1
gen japan_alt = japan
gen flag_for_mult_alt = flag_for_mult
replace japan = 1 if (japanU2008==1 & japanU2011==1) & flag_us_mult~=1
replace flag_for_mult = 1 if (flag_for_multU2011==1 & flag_for_multU2008==1)

**1.1 Ensure that the multinational flags are consistent
bys firmid: egen maxflagfor = max(flag_for_mult)
by firmid: egen maxflagus = max(flag_us_mult)
by firmid: egen maxflagjpn = max(japan)

replace flag_for_mult = maxflagfor
replace flag_us_mult = maxflagus
replace japan = maxflagjpn
drop maxflagjpn maxflagus maxflagfor

**Make sure Uniworld didn't screw anything up
replace japan = 0 if flag_us_mult==1
**For These: Enforce all indicators to be non-japanese

	replace japan=0 if firmid=="`fname'"
}
replace flag_for_mult=0 if firmid==""
replace flag_for_mult=0 if firmid==""

bys firmid year: egen sumjimp_int_rel = sum(jimp_rel_int)
bys firmid year: egen sumjimp_int_nrel = sum(jimp_nrel_int)
gen relshareyear = sumjimp_int_rel / (sumjimp_int_rel + sumjimp_int_nrel)
replace relshareyear = . if year~=2010
bys firmid: egen relshareyear2 = min(relshareyear)
drop relshareyear
rename relshareyear2 relshareyear
replace japan = 0 if japan==1 & japan_alt==0 & relshareyear<0.xx
drop relshareyear sumjimp_int_rel sumjimp_int_nrel

bys firmid year: egen sumimp_int_rel = sum(jimp_rel_int+njimp_rel_int)
bys firmid year: egen sumimp_int_nrel = sum(jimp_nrel_int+njimp_nrel_int)
gen trelshareyear = sumimp_int_rel / (sumimp_int_rel + sumimp_int_nrel)
replace trelshareyear = . if year~=2010
bys firmid: egen trelshareyear2 = min(trelshareyear)
drop trelshareyear
rename trelshareyear2 trelshareyear
drop if trelshare==0 & flag_for_mult==1 & flag_for_mult_alt==0
drop trelshareyear sumimp_int_rel sumimp_int_nrel

bys firmid year: egen sumjimp_int = sum(jimp_int)
replace sumjimp_int = . if year~=2010
bys firmid: egen sumjimp_int2 = min(sumjimp_int)
drop sumjimp_int
rename sumjimp_int2 sumjimp_int
replace japan = 0 if japan==1 & japan_alt==0 & sumjimp_int==0


cap gunzip analysisdata_manufU_Q.dta.gz
save analysisdata_manufU_Q.dta, replace
!gzip analysisdata_manufU_Q.dta

erase dca_lbd_2009_temp.dta
erase dca_lbd_2010_temp.dta
erase dca_lbd_2011_temp.dta
erase dca_lbd_2012_temp.dta
erase dca_lbd_2009_quarter.dta
erase dca_lbd_2010_quarter.dta
erase dca_lbd_2011_quarter.dta
erase dca_lbd_2012_quarter.dta
erase DCA_LBD_LFTTD_2009_quarter.dta
erase DCA_LBD_LFTTD_2010_quarter.dta
erase DCA_LBD_LFTTD_2011_quarter.dta
erase DCA_LBD_LFTTD_2012_quarter.dta
forvalues i = 2009(1)2012 {
	erase imp_firm`j'quarterly_clean.dta
	erase exp_firm`j'quarterly_clean.dta
}
